package com.imy.touch.runnable;

import android.os.SystemClock;
import android.view.ViewConfiguration;

import com.imy.touch.TouchCore;
import com.imy.touch.bean.GesturePoint;
import com.imy.touch.root.client.SocketClient;
import com.imy.touch.root.command.ProtocolCreate;

public class SwipeGestureRunnable extends BaseGestureRunnable implements Runnable {
  private GesturePoint end;
  private int step;
  private int time;

  public SwipeGestureRunnable(GesturePoint start, GesturePoint end, int step, int time) {
    super(start);
    this.end = end;
    this.step = step;
    this.time = time;
  }

  public SwipeGestureRunnable(GesturePoint point) {
    super(point);
  }

  @Override
  public void run() {
    //向下滑动还是向上
    if (checkUserRoot()) {
      int swipeSteps = step;
      double xStep = 0;
      double yStep = 0;

      // avoid a divide by zero
      if (swipeSteps == 0) {
        swipeSteps = 1;
      }
      float endx = end.getX();
      float endy = end.getY();
      float startX = mPoint.getX();
      float startY = mPoint.getY();
      xStep = ((double) (endx - startX)) / swipeSteps;
      yStep = ((double) (endy - startY)) / swipeSteps;
      int sleep_time = time / swipeSteps;
      // first touch starts exactly at the point requested
      getClient().getTransceiver().send(ProtocolCreate.createDown((int) startX, (int) startY, 0));
      SystemClock.sleep(sleep_time);
      for (int i = 0; i < swipeSteps; i++) {
        float x = startX + (int) (xStep * i);
        float y = startY + (int) (yStep * i);
        getClient().getTransceiver().send(ProtocolCreate.createMove((int)x,(int)y,0));
        SystemClock.sleep(sleep_time);
      }
      getClient().getTransceiver().send(ProtocolCreate.createUp((int)endx,(int)endy,0));

    }
  }
}
